package com.city.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.city.eduservice.entity.EduSubject;
import com.city.eduservice.entity.excel.SubjectData;
import com.city.eduservice.service.EduSubjectService;
import com.city.servicebase.myexception.MyException;
import com.city.servicebase.myexception.exceptionenum.MyExceptionEnumImpl;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/6/19
 * Time: 0:24
 * Description: No Description
 *
 * 改监听器不能放进spring ioc中因为要自己手动用到的时候才new，因为没放进ioc所以这里读取到数据后、
 * 无法@Autowired
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    private EduSubjectService eduSubjectService;

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    public SubjectExcelListener() {
    }


    //一行一行读取数据
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null) {
            throw new MyException(MyExceptionEnumImpl.EXCEL_NOT_DATA);
        }
        //行读取的第一个值为一级分类，第二个为二级分类，去重
        EduSubject oneEduSubject = getOneEduSubject(subjectData.getOneSubjectName());
        if (oneEduSubject == null) {
            oneEduSubject=new EduSubject();
            //new的实际意义，如果为空，则返回null，并不是edusubject对象，所以无法赋值操作,使用mybatisplus时，new对象自动使用雪花算法把id值赋上，来插入数据库，而不是由数据库生成
            //使这个对象有id值,必须new，因为数据库设置了不为空，又没设置自动增值，所以不new的话插入的对象没id
            oneEduSubject.setParentId("0");
            oneEduSubject.setTitle(subjectData.getOneSubjectName());
            eduSubjectService.save(oneEduSubject);
        }

        String parentId = oneEduSubject.getId();//无论这个值有没有，mybatisplus都会为这个new出来的对象赋id值
        EduSubject twoEduSubject = getTwoEduSubject(subjectData.getTwoSubjectName(), parentId);

        if (twoEduSubject == null) {
            twoEduSubject=new EduSubject();
            twoEduSubject.setTitle(subjectData.getTwoSubjectName());
            twoEduSubject.setParentId(parentId);
            eduSubjectService.save(twoEduSubject);
        }
    }

    //一级分类拿出来插入时去重
    private EduSubject getOneEduSubject(String oneEduSubjectTitle){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        queryWrapper.eq("title",oneEduSubjectTitle);
        EduSubject one = eduSubjectService.getOne(queryWrapper);
        return one;
    }

    //二级分类拿出来插入时去重
    private EduSubject getTwoEduSubject(String TwoEduSubjectTitle,String parentId){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.eq("title",TwoEduSubjectTitle);
        EduSubject one = eduSubjectService.getOne(queryWrapper);
        return one;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
